﻿using AntdUI;
using PosClient.ViewModel;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PosClient.View
{
    public partial class DownLoadView : AntdUI.Window, IViewFor<DownLoadViewModel>
    {
        object IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (DownLoadViewModel)value;
        }

        public DownLoadViewModel ViewModel { get; set; }

        public DownLoadView()
        {
            InitializeComponent();

            InitColumns();

            InitBinding();
        }

        private void InitColumns()
        {
            table_base.Columns = new AntdUI.ColumnCollection()
            {
                new Column("JobName", "任务编码",ColumnAlign.Center),
                new Column("JobNote", "任务名称",ColumnAlign.Center),
                new Column("JobBody", "任务组",ColumnAlign.Center),
                new Column("JobClass", "任务类名",ColumnAlign.Center),
                new Column("JobCron", "任务Cron",ColumnAlign.Center),
                new Column("JobStatus", "任务状态",ColumnAlign.Center),
            };
        }

        private void InitBinding()
        {
            ViewModel = new DownLoadViewModel();
            this.WhenActivated(d =>
            {
                this.Bind(ViewModel,
                    vm => vm.Index,
                    v => v.table_base.SelectedIndex)
                .DisposeWith(d);

                this.Events().Load.Do(x => ViewModel.GetJobCommand.Execute().Subscribe()).Subscribe().DisposeWith(d);

                this.Events().KeyDown.Do(x =>
                {
                    if (x.KeyData == Keys.Enter)
                    {
                        if (table_base.SelectedIndex > 0)
                        {
                            var job = ViewModel.JobList[table_base.SelectedIndex - 1];
                            var config = new AntdUI.Modal.Config(Globals.MainForm, "提醒", $"是否下载 {job.JobNote} 数据");
                            var result = AntdUI.Modal.open(config);
                            if (result == DialogResult.OK)
                            {
                                ViewModel.RunJobCommand.Execute(job.JobName).Subscribe();
                            }
                        }
                    }
                    else if (x.KeyData == Keys.Escape)
                    {
                        this.Close();
                    }
                }).Subscribe().DisposeWith(d);

            });

            table_base.Binding(ViewModel.JobList);
        }


    }
}
